package github.flandre.examplemod.common.entity;

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.*;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.ai.goal.*;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.datasync.DataParameter;
import net.minecraft.network.datasync.DataSerializers;
import net.minecraft.network.datasync.EntityDataManager;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.IServerWorld;
import net.minecraft.world.World;

import javax.annotation.Nullable;


public class ExampleEntity extends CreatureEntity {
    private static final EntityType<ExampleEntity> TYPE = EntityType.Builder.<ExampleEntity>create(
            ExampleEntity::new,
            EntityClassification.MONSTER //指定为怪物
    ).size(1.2f,1.95f).build("example_entity");


    //给实体添加字段使用EntityDataManger管理对象 要传递的数据为byte
    private static final DataParameter<Byte> COLOR =
            EntityDataManager.createKey(ExampleEntity.class, DataSerializers.BYTE);
    //存储颜色的字段

    public static EntityType<ExampleEntity> getEntityType()
    {
        return TYPE;
    }

    public Byte getCOLOR()
    {
        return this.getDataManager().get(COLOR);
    }
    //类加载的时候注册数据的初始化 防止出现空值
    @Override
    protected void registerData() {
        super.registerData();
        this.getDataManager().register(COLOR,(byte)0);
    }
    //生物出生的时候初始化
    @Nullable
    @Override
    public ILivingEntityData onInitialSpawn(IServerWorld worldIn, DifficultyInstance difficultyIn, SpawnReason reason, @Nullable ILivingEntityData spawnDataIn, @Nullable CompoundNBT dataTag) {
        this.getDataManager().set(COLOR,(byte)rand.nextInt(3)); //在0-3之间生成一个随机数
        return super.onInitialSpawn(worldIn, difficultyIn, reason, spawnDataIn, dataTag);
    }
    //保存数据
    @Override
    public void writeAdditional(CompoundNBT compound) {
        super.writeAdditional(compound);
        compound.putByte("color",this.getCOLOR());
    }

    @Override
    public void readAdditional(CompoundNBT compound) {
        super.readAdditional(compound);
        this.getDataManager().set(COLOR, compound.getByte("color"));
    }


    public ExampleEntity(World worldIn)
    {
        this(null,worldIn);
    }
    public ExampleEntity(EntityType<? extends CreatureEntity> type, World worldIn)
    {
        super(type, worldIn);
    }

    //添加属性
    public static AttributeModifierMap.MutableAttribute func_234192_eI_() {
        return MobEntity.func_233666_p_().createMutableAttribute(
                Attributes.MOVEMENT_SPEED, (double)0.3F).
                createMutableAttribute(Attributes.MAX_HEALTH, 10.0D).
                createMutableAttribute(Attributes.ATTACK_DAMAGE, 2.0D)
                ;
    }

    //AI行为
    @Override
    protected void registerGoals() {
        super.registerGoals();
        // 优先级 行为
        //优先0 游泳
        this.goalSelector.addGoal(0,new SwimGoal(this));
        //近战攻击
        this.goalSelector.addGoal(1,new MeleeAttackGoal(this,1.0,true));
        //绕开水行走
        this.goalSelector.addGoal(2,new WaterAvoidingRandomWalkingGoal(this,0.8));
        //看向8格内的玩家
        this.goalSelector.addGoal(4,new LookAtGoal(this, PlayerEntity.class,8));
        //随机看
        this.goalSelector.addGoal(5,new LookRandomlyGoal(this));
        //反击 + CallsForHelp僵尸猪人围攻特性
        this.goalSelector.addGoal(0,new HurtByTargetGoal(this).setCallsForHelp());
        //自定义
        this.goalSelector.addGoal(3,new ChangGlassToDiamond(this));

    }
    //行走的时候会把草方块变成泥土
    private static class ChangGlassToDiamond extends Goal{

        private final ExampleEntity entity;
        public ChangGlassToDiamond(ExampleEntity entity)
        {
            this.entity = entity;
        }
        //具体执行
        @Override
        public void startExecuting() {
            super.startExecuting();
            //返回为真的话，这边就会运行
            BlockPos blockPos = new BlockPos(entity.getPosX(),entity.getPosY()-1,entity.getPosZ());
            Block Block;
            switch ((int)(1+Math.random()*(6-1+1)))
            {
                case 1:
                    Block = Blocks.DIAMOND_BLOCK;
                    break;
                case 2:
                    Block = Blocks.IRON_BLOCK;
                    break;
                case 3:
                    Block = Blocks.GOLD_BLOCK;
                    break;
                case 4:
                    Block = Blocks.EMERALD_BLOCK;
                    break;
                case 5:
                    Block = Blocks.REDSTONE_BLOCK;
                    break;
                default:
                    Block = Blocks.LAPIS_BLOCK;
                    break;
            }
            this.entity.world.setBlockState(blockPos, Block.getDefaultState());
        }

        //将要执行
        @Override
        public boolean shouldExecute() {
            //获取位置
            BlockPos blockPos = new BlockPos(entity.getPosX(),entity.getPosY()-0.2,entity.getPosZ());
            //判断脚底下的方块是否为草方块
            return this.entity.world.getBlockState(blockPos).getBlock() == Blocks.GRASS_BLOCK;
        }
    }


}

//实体的注册在EntityInit中 实体的渲染在event ClientEvent中